<template>
  <div class="bg-white overflow-hidden dictionary">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">新增</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction :actions="[
            {
              tooltip: '修改',
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              tooltip: '删除',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              onClick: handleDeleteStop.bind(null, record),
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
                placement: 'left'
              },
            },
          ]" />
        </template>
      </template>
    </BasicTable>
    <UserModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { list, del } from '/@/api/privilege/sysdatarange';
import { PageWrapper } from '/@/components/Page';
import { useModal } from '/@/components/Modal';
import { columns } from './user.data';
import UserModal from './UserModal.vue';

export default defineComponent({
  name: 'UserTable',
  components: { BasicTable, UserModal, PageWrapper, TableAction },
  setup(_, { emit }) {
    const [registerModal, { openModal, setModalProps }] = useModal();
    const roleId = ref<string>('');
    const [registerTable, { reload, setProps }] = useTable({
      title: '管理员列表',
      api: list,
      columns,
      formConfig: {
        labelWidth: 120,
        showAdvancedButton: false,
        showResetButton: false,
        autoSubmitOnEnter: true,
      },
      immediate: false,
      showIndexColumn: false,
      showTableSetting: false,
      bordered: true,
      pagination: true,
      rowKey: 'id',
      actionColumn: {
        width: 80,
        title: '操作',
        dataIndex: 'action',
      },
    });
    function handleCreate() {
      setModalProps({ title: '新增管理员' });
      openModal(true, {
        record: { roleId: roleId.value },
        isUpdate: false,
      });
    }

    function filterByRoleType(roleIdOut) {
      roleId.value = roleIdOut;
      setProps({ searchInfo: { roleId: roleId } })
      reload({ page: 1 });
    }


    function handleEdit(record: Recordable, e) {
      e.stopPropagation();
      setModalProps({ title: '修改管理员' });
      openModal(true, {
        record,
        isUpdate: true,
      });
    }
    function handleDeleteStop(record: Recordable, e) {
      e.stopPropagation();
    }

    function handleDelete(record: Recordable) {
      del(record.id).then(() => {
        reload();
      });
    }

    function handleSuccess() {
      setTimeout(() => {
        reload();
      }, 200);
    }


    return {
      registerTable,
      registerModal,
      roleId,
      handleDeleteStop,
      filterByRoleType,
      handleCreate,
      handleEdit,
      handleDelete,
      handleSuccess,
    };
  },
});
</script>

<style lang="less">
.dictionary {
  .vben-basic-table-form-container {
    padding: 0;

    .vben-basic-form {
      margin-bottom: 0;
    }
  }
}
</style>
